package com.andorid.Download;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

// class khoi tao ket noi toi server dung http connection
public class ketnoi  {
	public URL url;
	public URLConnection con;
	public String chuoiText;// bien nay de lay file text trong input
	//bien total de lay kick co cua file
	public int totalSize;
	public int len;
	public InputStream in;
	public int respone=-1;
	public ProgressDialog pDialog;
	public InputStream ketnoiHTTP(String url1)
	{	
		try {
			url= new URL(url1);
			con=url.openConnection();
			// thiet lap ket noi http
			HttpURLConnection httpcon= (HttpURLConnection)con;
			httpcon.setAllowUserInteraction(false);
			httpcon.setInstanceFollowRedirects(true);
			httpcon.setRequestMethod("GET");
			httpcon.setDoInput(true);
			httpcon.setDoOutput(true);
			//connect 
			httpcon.connect();
			Log.d("KQ0","ketnoitc");
			// nhan ket qua tra ve 
			respone=httpcon.getResponseCode();
			totalSize=httpcon.getContentLength();
			if(respone==HttpURLConnection.HTTP_BAD_GATEWAY)
			{
				
			}
			else if(respone==HttpURLConnection.HTTP_OK)
			{
				// neu ket qua tra ve ok thi lay du lieu
				in=httpcon.getInputStream();
				totalSize=httpcon.getContentLength();
				Log.d("KQ1","nhan du lieu thanh cong");
			}
				
		} catch (Exception e) {
			// TODO: handle exception
		}
		return in;
	}
	//Ham download anh ve dien thoai
	public Bitmap DownloadImage (String url)
	{
		Bitmap bmap=null;
		try {
			Log.d("KQ","form2");
			in=ketnoiHTTP(url);
			bmap=BitmapFactory.decodeStream(in);
			in.close();
		} catch (Exception e) {
			// TODO: handle exception
		}
		return bmap;
		
	}
	//Ham download file 
	public void DownloadFile(String url,String path,String name,String duoi)
	{
		String PATH=path;
		String nameFile=name+duoi;
		File file = new File(PATH);
		file.mkdirs();
		//File fileName= new File(nameFile);
		//tao file co ten la nameFile va co duong dan la file
		File output= new File(file, nameFile);
	    Log.v("", "PATH: " + PATH);
		try {
			in=ketnoiHTTP(url);
			Log.d("KQ3","thanh cong");
			FileOutputStream fos = new FileOutputStream(output);
			InputStreamReader isr= new InputStreamReader(in);
			Log.i("lengthOfFile", Integer.toString(totalSize));
			// dung mang byte de doc input thi ko can input stream reader
			// mang char thif phai co input stream reader 
			byte[] buffer = new byte[1024];
			char[] charBuffer = new char[1024];
			len= 0;
			while((len=in.read(buffer))!=-1)
			{   
				
				/*myProgress = (int)((len/lengthOfFile)*100);
            	myProgress = myProgress + myProgress;
            	
            	Log.i("My Progress", Integer.toString(myProgress));
            	publishProgress(myProgress);
				 */
				//ghi du lieu vao file
				fos.write(buffer,0,len);
				fos.flush();
			}	
			fos.close();
			in.close();
		} catch (IOException e) {
	        e.printStackTrace();

		}


	}
}
